feat(decorators): support RegExp instances in @ApiProperty({ pattern })#3787
feat(decorators): support RegExp instances in @ApiProperty({ pattern })#3787temrjan wants to merge 2 commits intonestjs:masterfrom
Conversation
Automatically extract .source from RegExp values passed to the pattern option, so developers can use @ApiProperty({ pattern: /^[a-z]+$/ }) instead of manually calling .source. Fixes nestjs#3719 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR improves @ApiProperty({ pattern }) developer ergonomics by accepting RegExp instances and ensuring they serialize into the OpenAPI schema as a string pattern.
Changes:
- Extend
SchemaObjectCommonMetadatato allowpattern?: string | RegExp. - In
createApiPropertyDecorator, convertoptions.patternfromRegExpto its.sourcestring before storing metadata.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
lib/interfaces/schema-object-metadata.interface.ts |
Widen pattern typing to support RegExp while keeping OpenAPI SchemaObject compatibility via Omit<..., 'pattern'>. |
lib/decorators/api-property.decorator.ts |
Add runtime normalization so RegExp patterns are stored as strings in decorator metadata. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (options.pattern instanceof RegExp) { | ||
| options.pattern = options.pattern.source; | ||
| } |
There was a problem hiding this comment.
This adds new runtime behavior (converting options.pattern when it’s a RegExp), but there doesn’t appear to be a test asserting the generated schema’s pattern value. Please add a test that defines a DTO property with @ApiProperty({ pattern: /^[+]?abc$/ }) (and ideally one with flags, e.g. /abc/i) and verifies the resulting OpenAPI schema contains pattern: '^[+]?abc$' (and that flags are not emitted).
Cover RegExp-to-string conversion, flag stripping, and string passthrough in the OpenAPI schema generated by SchemaObjectFactory. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary
RegExpinstances to their.sourcestring when passed to@ApiProperty({ pattern })SchemaObjectCommonMetadatato acceptpattern?: string | RegExpTest plan
tsc) passes with no new errorsanytypes introducedinstanceof RegExpFixes #3719
🤖 Generated with Claude Code